﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using System.Threading;
using l1cs.server.model.skill;

namespace l1cs.server.model.poison
{
    public class L1DamagePoison : L1Poison
    {
        private static Logger _log = Logger.getLogger( typeof( L1DamagePoison ).FullName );

        private Timer _timer;
        private readonly L1Character _attacker;
        private readonly L1Character _target;
        private readonly int _damageSpan;
        private readonly int _damage;

        private L1DamagePoison( L1Character attacker, L1Character cha, int damageSpan, int damage )
        {
            _attacker = attacker;
            _target = cha;
            _damageSpan = damageSpan;
            _damage = damage;

            doInfection();
        }

        //private class NormalPoisonTimer extends Thread {
        //    override
        public void NormalPoisonTimerSub( object state )
        {
            while ( true )
            {
                //try {
                Thread.Sleep( _damageSpan );
                //} catch (InterruptedException e) {
                //    break;
                //}

                if ( !_target.hasSkillEffect( L1SkillId.STATUS_POISON ) )
                {
                    break;
                }
                if ( _target is L1PcInstance )
                {
                    L1PcInstance player = (L1PcInstance)_target;
                    player.receiveDamage( _attacker, _damage, false );
                    if ( player.isDead() )
                    { // 死亡したら解毒処理
                        break;
                    }
                }
                else if ( _target is L1MonsterInstance )
                {
                    L1MonsterInstance mob = (L1MonsterInstance)_target;
                    mob.receiveDamage( _attacker, _damage );
                    if ( mob.isDead() )
                    { // 死亡しても解毒しない
                        return;
                    }
                }
            }
            cure(); // 解毒処理
        }
        //}

        bool isDamageTarget( L1Character cha )
        {
            return ( cha is L1PcInstance ) || ( cha is L1MonsterInstance );
        }

        private void doInfection()
        {
            _target.setSkillEffect( L1SkillId.STATUS_POISON, 30000 );
            _target.setPoisonEffect( 1 );

            if ( isDamageTarget( _target ) )
            {
                //_timer = new NormalPoisonTimer();
                //GeneralThreadPool.getInstance().execute( _timer ); // 通常毒タイマー開始
                _timer = new Timer( new TimerCallback( NormalPoisonTimerSub ), null, 0, Timeout.Infinite );
            }
        }

        public static bool doInfection( L1Character attacker, L1Character cha, int damageSpan, int damage )
        {
            if ( !isValidTarget( cha ) )
            {
                return false;
            }

            cha.setPoison( new L1DamagePoison( attacker, cha, damageSpan, damage ) );
            return true;
        }


        override public int getEffectId()
        {
            return 1;
        }

        override public void cure()
        {
            if ( _timer != null )
            {
                _timer.Dispose(); // 毒タイマー解除
            }

            _target.setPoisonEffect( 0 );
            _target.killSkillEffectTimer( L1SkillId.STATUS_POISON );
            _target.setPoison( null );
        }
    }
}
